package com.zwps.biz.domain.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwps.biz.api.model.dto.mointordentify.MointorCountVO;
import com.zwps.biz.api.model.dto.mointordentify.MonitorAlarmRecordIdentifyDTO;
import com.zwps.biz.api.model.dto.mointordentify.MonitorUpdateDTO;
import com.zwps.biz.api.model.vo.mointordentify.MonitDentifyListVO;
import com.zwps.biz.api.model.vo.mointordentify.MonitDentifyTrendVO;
import com.zwps.biz.api.model.vo.mointordentify.MonitDentigyTypeVO;
import com.zwps.biz.api.model.vo.mointordentify.MonitorAlarmRecordIdentifyVO;
import com.zwps.biz.api.model.vo.pubpointall.PubPointCountVO;
import com.zwps.biz.dal.db.dao.IotMonitorDeviceDAO;
import com.zwps.biz.dal.db.dao.MonitorAlarmRecordIdentifyDAO;
import com.zwps.biz.dal.db.object.MonitorAlarmRecordIdentifyDO;
import com.zwps.biz.domain.enums.IdentifyTypeEnum;
import com.zwps.biz.domain.enums.InvasionTypeEnum;
import com.zwps.biz.domain.service.MonitorAlarmRecordIdentifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 *
 */
@Service
public class MonitorAlarmRecordIdentifyServiceImpl extends ServiceImpl<MonitorAlarmRecordIdentifyDAO, MonitorAlarmRecordIdentifyDO>
        implements MonitorAlarmRecordIdentifyService {

    @Autowired
    private MonitorAlarmRecordIdentifyDAO identifyDAO;
    @Autowired
    private IotMonitorDeviceDAO deviceDAO;

    @Override
    public List<MonitorAlarmRecordIdentifyVO> getCount(MointorCountVO countVO) {
        // 如果 startTime 和 endTime 都为空，则获取最近一个月的时间范围
        if (Objects.isNull(countVO.getStartTime()) && Objects.isNull(countVO.getEndTime())) {
            Calendar calendar = Calendar.getInstance();
            Date endDate = calendar.getTime();
            calendar.add(Calendar.MONTH, -1);
            Date startDate = calendar.getTime();
            countVO.setStartTime(startDate);
            countVO.setEndTime(endDate);
        }
        List<MonitorAlarmRecordIdentifyVO> vos = identifyDAO.getCount(countVO);
        for (MonitorAlarmRecordIdentifyVO vo : vos) {
            vo.setName(IdentifyTypeEnum.getDescriptionByType(vo.getType()));
        }
        return vos;
    }

    @Override
    public  IPage<MonitDentifyListVO> getIdentify(MonitorAlarmRecordIdentifyDTO identifyDTO) {
        if (Objects.isNull(identifyDTO.getStartTime()) && Objects.isNull(identifyDTO.getEndTime())) {
            Calendar calendar = Calendar.getInstance();
            Date endDate = calendar.getTime();
            calendar.add(Calendar.MONTH, -1);
            Date startDate = calendar.getTime();
            identifyDTO.setStartTime(startDate);
            identifyDTO.setEndTime(endDate);
        }
        //分页
        if (Objects.isNull(identifyDTO.getPageNum()) || identifyDTO.getPageNum() <= 0) {
            identifyDTO.setPageNum(1);
        }
        if (Objects.isNull(identifyDTO.getPageSize()) || identifyDTO.getPageSize() <= 0) {
            identifyDTO.setPageSize(10);
        }
        Page<MonitDentifyListVO> page = new Page<>(identifyDTO.getPageNum(), identifyDTO.getPageSize());

        IPage<MonitDentifyListVO> resultPage = identifyDAO.getIdentify(page, identifyDTO);
        for (MonitDentifyListVO vo : resultPage.getRecords()) {
            // 使用 monitor 数据源查询 device 名称
            String deviceName = deviceDAO.queryBydeviceId(vo.getDeviceId());
            vo.setName(deviceName);  // 设置 device 名称
        }
        return resultPage;
    }

    @Override
    public List<MonitDentigyTypeVO> getTypes() {
//     return identifyDAO.getTypes();
        return InvasionTypeEnum.getAll();
    }

    @Override
    public List<MonitDentigyTypeVO> getMonitorTypes() {
        return identifyDAO.getMonitorTypes();
    }

    @Override
    public  List<MonitDentifyTrendVO> getTrend(MointorCountVO countVO) {
        if (Objects.isNull(countVO.getStartTime()) && Objects.isNull(countVO.getEndTime())) {
            Calendar calendar = Calendar.getInstance();
            Date endDate = calendar.getTime();
            calendar.add(Calendar.MONTH, -1);
            Date startDate = calendar.getTime();
            countVO.setStartTime(startDate);
            countVO.setEndTime(endDate);
        }

        return identifyDAO.getTrend(countVO);
    }

    @Override
    public List<MonitDentifyListVO> getRecently() {
        List<MonitDentifyListVO> recently = identifyDAO.getRecently();
        recently.forEach(vo -> {
             vo.setName(deviceDAO.queryBydeviceId(vo.getDeviceId()));
        });
        return recently;
    }

    @Override
    public IPage<MonitDentifyListVO> getEvent(MonitorAlarmRecordIdentifyDTO dto) {
        //分页
        if (Objects.isNull(dto.getPageNum()) || dto.getPageNum() <= 0) {
            dto.setPageNum(1);
        }
        if (Objects.isNull(dto.getPageSize()) || dto.getPageSize() <= 0) {
            dto.setPageSize(10);
        }
        Page<MonitDentifyListVO> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        IPage<MonitDentifyListVO> resultPage = identifyDAO.getEvent(page, dto);
        for (MonitDentifyListVO vo : resultPage.getRecords()) {
            // 使用 monitor 数据源查询 device 名称
            String deviceName = deviceDAO.queryBydeviceId(vo.getDeviceId());
            vo.setName(deviceName);
        }
        return resultPage;
    }

    @Override
    public MonitDentifyListVO getEventByAramId(String id) {
        MonitDentifyListVO vo = identifyDAO.getEventByAramId(id);
        if (Objects.nonNull(vo)) {
            vo.setName(deviceDAO.queryBydeviceId(vo.getDeviceId()));
        }
        return vo;
    }

    @Override
    public boolean updateOfId(MonitorUpdateDTO dto) {
        return  identifyDAO.updateOfId( dto);
    }

    @Override
    public List<PubPointCountVO> countsDevice(String deviceType) {
        List<PubPointCountVO> list=new ArrayList<>();
        String[] split = deviceType.split(",");
        for (String s : split) {
            PubPointCountVO vo=  deviceDAO.countsDevice(s);
            if ("1".equals(s)){
            vo.setPointName("监控");
            vo.setSttpcd(0);
            }
            list.add(vo);

        }
        return list;
    }
}




